#include "bridge_service.h"
#include <stdio.h>
#include <stdlib.h>
#include <libubus.h>
#include <libubox/uloop.h>
#include <libubox/list.h>
#include <libubox/blobmsg_json.h>
#include <cjson/cJSON.h>
#include "comm.h"
#include "data_type.h"
#include "trans_util.h"

extern t_RunCfg runCfg;
struct blob_buf lte_buf;

static const struct blobmsg_policy lte_info_policy[] = {
    [0] = { .name = "cgmi",           .type = BLOBMSG_TYPE_STRING },
    [1] = { .name = "cgmm",        .type = BLOBMSG_TYPE_STRING },
    [2] = { .name = "gsn",   .type = BLOBMSG_TYPE_STRING },
    [3] = { .name = "qccid",   .type = BLOBMSG_TYPE_STRING },
    [4] = { .name = "signalPower",   .type = BLOBMSG_TYPE_INT32 },
    [5] = { .name = "signalQuality",   .type = BLOBMSG_TYPE_INT32 },
    [6] = { .name = "bytes_sent",   .type = BLOBMSG_TYPE_INT32 },
    [7] = { .name = "bytes_recv",   .type = BLOBMSG_TYPE_INT32 },
    [8] = { .name = "cgpaddr",   .type = BLOBMSG_TYPE_STRING },
    [9] = { .name = "creg",   .type = BLOBMSG_TYPE_INT32 },
    [10] = { .name = "state",   .type = BLOBMSG_TYPE_STRING },
    [11] = { .name = "RAT",   .type = BLOBMSG_TYPE_STRING },
    [12] = { .name = "is_tdd",   .type = BLOBMSG_TYPE_STRING },
    [13] = { .name = "MCC",   .type = BLOBMSG_TYPE_INT32 },

    [14] = { .name = "MNC",   .type = BLOBMSG_TYPE_INT32 },
    [15] = { .name = "cellID",   .type = BLOBMSG_TYPE_INT32 },
    [16] = { .name = "EARFCN",   .type = BLOBMSG_TYPE_INT32 },
    [17] = { .name = "UL_bandwidth",   .type = BLOBMSG_TYPE_INT32 },
    [18] = { .name = "DL_bandwidth",   .type = BLOBMSG_TYPE_INT32 },

    [19] = { .name = "TAC",   .type = BLOBMSG_TYPE_INT32 },
    [20] = { .name = "PCI",   .type = BLOBMSG_TYPE_INT32 },
    [21] = { .name = "RSRP",   .type = BLOBMSG_TYPE_INT32 },
    [22] = { .name = "RSRQ",   .type = BLOBMSG_TYPE_INT32 },
    [23] = { .name = "RSSI",   .type = BLOBMSG_TYPE_INT32 },
    [24] = { .name = "SINR",   .type = BLOBMSG_TYPE_INT32 },
    [25] = { .name = "srxlev",   .type = BLOBMSG_TYPE_INT32 },
    [26] = { .name = "tx_porwr",   .type = BLOBMSG_TYPE_INT32 },
};

/**
 * 获取端口状态数据
 */
static void lte_info_callback(struct ubus_request *req, int type, struct blob_attr *msg) {

    if (!msg) {
        return;
    }
    cJSON *lteInfo = (cJSON *)req->priv;
    // 解析内存数据
    struct blob_attr *tb[27];
    blobmsg_parse(lte_info_policy, ARRAY_SIZE(lte_info_policy), tb, blobmsg_data(msg), blobmsg_data_len(msg));

    if(tb[0]) EP_JSON_SET_STR(lteInfo, "cgmi", blobmsg_get_string(tb[0]));
    if(tb[1]) EP_JSON_SET_STR(lteInfo, "cgmm", blobmsg_get_string(tb[1]));
    if(tb[2]) EP_JSON_SET_STR(lteInfo, "gsn", blobmsg_get_string(tb[2]));
    if(tb[3]) EP_JSON_SET_STR(lteInfo, "qccid", blobmsg_get_string(tb[3]));
    if(tb[4]) EP_JSON_SET_INT(lteInfo, "signalPower", blobmsg_get_u32(tb[4]));
    if(tb[5]) EP_JSON_SET_INT(lteInfo, "signalQuality", blobmsg_get_u32(tb[5]));
    if(tb[6]) EP_JSON_SET_INT(lteInfo, "bytes_sent", blobmsg_get_u32(tb[6]));
    if(tb[7]) EP_JSON_SET_INT(lteInfo, "bytes_recv", blobmsg_get_u32(tb[7]));
    if(tb[8]) EP_JSON_SET_STR(lteInfo, "cgpaddr", blobmsg_get_string(tb[8]));
    if(tb[9]) EP_JSON_SET_INT(lteInfo, "creg", blobmsg_get_u32(tb[9]));
    if(tb[10]) EP_JSON_SET_STR(lteInfo, "state", blobmsg_get_string(tb[10]));
    if(tb[11]) EP_JSON_SET_STR(lteInfo, "RAT", blobmsg_get_string(tb[11]));
    if(tb[12]) EP_JSON_SET_STR(lteInfo, "is_tdd", blobmsg_get_string(tb[12]));
    if(tb[13]) EP_JSON_SET_INT(lteInfo, "MCC", blobmsg_get_u32(tb[13]));
    if(tb[14]) EP_JSON_SET_INT(lteInfo, "MNC", blobmsg_get_u32(tb[14]));

    if(tb[15]) EP_JSON_SET_INT(lteInfo, "cellID", blobmsg_get_u32(tb[15]));
    if(tb[16]) EP_JSON_SET_INT(lteInfo, "EARFCN", blobmsg_get_u32(tb[16]));
    if(tb[17]) EP_JSON_SET_INT(lteInfo, "UL_bandwidth", blobmsg_get_u32(tb[17]));
    if(tb[18]) EP_JSON_SET_INT(lteInfo, "DL_bandwidth", blobmsg_get_u32(tb[18]));
    if(tb[19]) EP_JSON_SET_INT(lteInfo, "TAC", blobmsg_get_u32(tb[19]));
    if(tb[20]) EP_JSON_SET_INT(lteInfo, "PCI", blobmsg_get_u32(tb[20]));
    if(tb[21]) EP_JSON_SET_INT(lteInfo, "RSRP", blobmsg_get_u32(tb[21]));
    if(tb[22]) EP_JSON_SET_INT(lteInfo, "RSRQ", blobmsg_get_u32(tb[22]));
    if(tb[23]) EP_JSON_SET_INT(lteInfo, "RSSI", blobmsg_get_u32(tb[23]));
    if(tb[24]) EP_JSON_SET_INT(lteInfo, "SINR", blobmsg_get_u32(tb[24]));
    if(tb[25]) EP_JSON_SET_INT(lteInfo, "srxlev", blobmsg_get_u32(tb[25]));
    if(tb[26]) EP_JSON_SET_INT(lteInfo, "tx_porwr", blobmsg_get_u32(tb[26]));
}

int read_lte_status_data() {


    // 连接 ubus
    struct ubus_context *ctx = ubus_connect(NULL);
    if (!ctx) {
        fprintf(stderr, "Failed to connect to ubus\n");
        return -1;
    }
    // 查找 network.device 的 ID
    uint32_t obj_id;
    if (ubus_lookup_id(ctx, "sihan.lte", &obj_id) != 0) {
        fprintf(stderr, "Failed to lookup sihan.bridge\n");
        ubus_free(ctx);
        return -1;
    }

    cJSON *root = cJSON_CreateObject();
    EP_JSON_SET_STR(root, "sn", runCfg.sn);

    printf("LoRa Addr: %04X\n", runCfg.lora_addr);

    EP_JSON_SET_INT(root, "loraId", runCfg.lora_addr);
    EP_JSON_SET_INT(root, "dataType", 3);
    EP_JSON_SET_INT(root, "version", 1);

    cJSON *lteData = cJSON_CreateObject();
    blob_buf_init(&lte_buf, 0);
    // 发起异步调用
    int ret = ubus_invoke(ctx, obj_id, "lte1_info", lte_buf.head, lte_info_callback, (void *)lteData, 3000);
    if (ret == 0) {
        cJSON_AddItemToObject(root,"data",lteData);
        char *str = cJSON_Print(root);
        printf("%s\n", str);
        send_data_to_mqtt(str);
    }
    
    cJSON_Delete(root);
    
    // 清理资源
    blob_buf_free(&lte_buf);
    ubus_free(ctx);
    return 0;
}

//接收LoRa数据的线程
void *lte_worker(void *param)
{
    while (1)
    {
        read_lte_status_data();
        sleep(5 * 60);
    }
}

int lte_worker_start() {
    if(runCfg.lte_enable){
        pthread_t pid;
        pthread_create(&pid, NULL, (void *)lte_worker, NULL);
    }
}

